/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package communication;

import flexjson.JSONDeserializer;
import flexjson.JSONException;
import flexjson.JSONSerializer;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import model.Control;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import model.Event;

/**
 *
 * @author Fernando
 */
public class QEvent extends RequestServer {

    private String table_name = "event";
    private String json = "";
    private JSONArray array;
    private ArrayList<JSONObject> objList = new ArrayList<JSONObject>();
    private Integer idEvent = null;
    private Integer Control_idControl;
    private Integer Animal_idAnimal;

    public static void main(String[] args) throws IOException, JSONException, org.codehaus.jettison.json.JSONException {
        //USAGE
        /*GET ALL REGISTERS
        QEvent SV = new QEvent();
        
        ArrayList<Event> All = SV.GetAll();
        
        for (Event V : All){
        
        System.out.println(V.getAnimal_idAnimal());        
        }
        //*/
        /*GET ONE REGISTER
        QEvent SV = new QEvent(4);
        //OR: QVaccine SV = new QVaccine(name); 
        
        Event V = SV.GetObject(0); // index 0, the first returned value
        System.out.println(V.getAnimal_idAnimal());
        //*/
        /*DELETE
        QEvent QV = new QEvent();
        
        String R = QV.Delete(null, null, null);
        //*/
        /*UPDATE
        QEvent QV = new QEvent();

        String R = QV.Update(1, 2, 7);
        //*/
        /*INSERT 
        QEvent QV = new QEvent();
        
        Control c = new Control(8);
        
        String answer = QV.Insert(1, c);
        //*/
    }

    public JSONObject Serialize(Event V) throws JSONException, org.codehaus.jettison.json.JSONException {

        JSONSerializer serializer = new JSONSerializer();

        this.json = serializer.serialize(V);

        JSONObject obj = new JSONObject(this.json);

        obj.remove("class");
        return obj;
    }

    public String Insert(Integer animal, Control control) throws UnsupportedEncodingException, IOException, JSONException, org.codehaus.jettison.json.JSONException {

        Event V = new Event(animal, control.getIdControl());

        JSONObject obj = this.Serialize(V);

        obj.remove("idEvent");

        String post = this.post("[" + obj.toString() + "]", this.table_name);

        return post;
    }

    public String Update(int id, Integer idAnimal, Integer idControl) throws UnsupportedEncodingException, IOException, JSONException, org.codehaus.jettison.json.JSONException {

        Event V = new Event(id, idAnimal, idControl);
        this.idEvent = id;

        JSONObject obj = this.Serialize(V);
        
        obj.remove("idEvent");

        this.json = obj.toString();

        String parameters = PrepareParameters();

        System.out.println(this.json);
        System.out.println(parameters);

        String put = this.put(this.table_name, this.json, parameters);

        return put;
    }

    public String Delete(Integer id, Integer idAnimal, Integer idControl) throws IOException {

        this.idEvent = id;
        this.Animal_idAnimal = idAnimal;
        this.Control_idControl = idControl;

        String delete = this.delete(this.table_name, PrepareParameters());

        return delete;
    }

    private boolean SetJsonList() throws JSONException, org.codehaus.jettison.json.JSONException {

        if (this.json == "") {
            return false;
        }

        this.array = new JSONArray(this.json);

        for (int i = 0; i < array.length(); i++) {

            JSONObject tmp = array.getJSONObject(i);
            objList.add(tmp);
        }
        return true;
    }

    public Event GetObject(int index) throws JSONException, org.codehaus.jettison.json.JSONException {

        if (this.json == "") {
            Event V = new Event();
            return V;
        }
        this.array = new JSONArray(this.json);

        JSONObject obj = null;

        obj = objList.get(index);

        obj.put("class", Event.class.getCanonicalName());

        String djson = obj.toString();

        Event V = new JSONDeserializer<Event>().deserialize(djson);

        return V;
    }

    public ArrayList<Event> GetAll() throws JSONException, IOException, org.codehaus.jettison.json.JSONException {

        this.json = this.get(this.table_name, null);
        SetJsonList();

        ArrayList<Event> Varray = new ArrayList<Event>();

        for (int i = 0; i < objList.size(); i++) {

            Varray.add(GetObject(i));
        }
        return Varray;
    }

    public String PrepareParameters() {

        String control = "";
        String animal = "";
        String id = "";
        String ret = "";

        id = (this.idEvent != null) ? ("idEvent=" + this.idEvent) : "";
        control = (this.Control_idControl != null) ? ("Control_idControl="
                + this.Control_idControl.toString()) : "";

        animal = (this.Animal_idAnimal != null) ? ("Animal_idAnimal="
                + this.Animal_idAnimal.toString()) : "";

        if (!"".equals(id) || !"".equals(control) || !"".equals(animal)) {
            return "?" + id + "&" + control + "&" + animal;
        }
        return ret;
    }

    public QEvent(Integer idEvent) throws IOException, JSONException, org.codehaus.jettison.json.JSONException, org.codehaus.jettison.json.JSONException, org.codehaus.jettison.json.JSONException, org.codehaus.jettison.json.JSONException {

        super();

        if (idEvent != null) {

            this.idEvent = idEvent;
            this.json = this.get(this.table_name, this.PrepareParameters());
        } else {
            this.json = this.get(this.table_name, null);
        }
        SetJsonList();
    }

    public QEvent(Integer idEvent, Integer idAnimal) throws IOException, JSONException, org.codehaus.jettison.json.JSONException {
        super();

        if (idAnimal != null) {

            this.Animal_idAnimal = idAnimal;
            this.json = this.get(this.table_name, this.PrepareParameters());
        } else {
            this.json = this.get(this.table_name, null);
        }
        SetJsonList();
    }

    public QEvent() throws IOException, JSONException {
        super();
    }

    public Integer getAnimal_idAnimal() {
        return Animal_idAnimal;
    }

    public void setAnimal_idAnimal(Integer Animal_idAnimal) {
        this.Animal_idAnimal = Animal_idAnimal;
    }

    public Integer getControl_idControl() {
        return Control_idControl;
    }

    public void setControl_idControl(Integer Control_idControl) {
        this.Control_idControl = Control_idControl;
    }

    public JSONArray getArray() {
        return array;
    }

    public void setArray(JSONArray array) {
        this.array = array;
    }

    public Integer getIdEvent() {
        return idEvent;
    }

    public void setIdEvent(Integer idEvent) {
        this.idEvent = idEvent;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public ArrayList<JSONObject> getObjList() {
        return objList;
    }

    public void setObjList(ArrayList<JSONObject> objList) {
        this.objList = objList;
    }

    public String getTable_name() {
        return table_name;
    }

    public void setTable_name(String table_name) {
        this.table_name = table_name;
    }
}
